<?php
/**
 * Created by PhpStorm.
 * User: Rei
 * Date: 16/5/1
 * Time: 11:25
 * Mail: rei@king.moe
 */
namespace Admin\Controller;

use Think\Controller;

class TagController extends SessionController
{
    public function index()
    {
        $tags = M('topic')->where('')->select();
        $this->assign('tags', $tags);
        $this->display('/news/tags');
    }

    //添加话题
    public function add()
    {
        $name = check_str(I('name'));
        $user = M('topic')->where(array('name' => $name))->find();

        $data = array(
            'code' => 0,
            'message' => '未知错误'
        );

        if ($user) {
            $data['code'] = 1;
            $data['message'] = '该分类已存在';
        } else if (M('topic')->data("name=$name")->add()) {
            $data['code'] = 2;
            $data['message'] = '添加成功';
        }else{
            $data['code'] = 3;
            $data['message'] = '添加失败';
        }

        echo json_encode($data);
        return;
    }

    //删除话题
    public function delete()
    {
        $tid = check_str(I('tid'));
        $result = M('topic')->where(array("tid"=>$tid))->delete();

        $data = array(
            'code' => 0,
            'message' => '未知错误'
        );

        if ($result) {
            $data['code'] = 1;
            $data['message'] = '操作成功';
        }

        echo json_encode($data);
        return;
    }
}